function chooseFile() {
    return new Promise((resolve, reject) => {
        uni.chooseFile({
            count: 1,
            type: 'all',
            success: function(res) {
                resolve(res.tempFiles[0]);
            },
            fail: function(err) {
                reject(err);
            }
        });
    });
}
async function uploadToCloud(file,Path) {
    const cloudPath = `${Path}/${Date.now()}-${Math.floor(Math.random() * 1000)}-${file.name}`; // 定义云端路径
    try {
        const res = await uniCloud.uploadFile({
            cloudPath: cloudPath,
            filePath: file.path, // 文件路径
        });
        console.log('Upload result:', res);
        return res.fileID; // 返回fileID，可用于后续访问该文件
    } catch (e) {
        console.error('Error uploading file:', e);
        throw e;
    }
}
async function getTempFileURL(fileID) {
    try {
        const res = await uniCloud.getTempFileURL({
            fileList: [fileID]
        });
        console.log('Temporary URL:', res.fileList[0].tempFileURL);
        return res.fileList[0].tempFileURL;
    } catch (e) {
        console.error('Error getting temporary URL:', e);
        throw e;
    }
}
/**
 * @param {string } 云端地址"项目/主体名"


*/
export default async function selectAndUploadFile(cloudPath) {
    try {
        const chosenFile = await chooseFile();
        const fileID = await uploadToCloud(chosenFile,cloudPath);
        const tempUrl = await getTempFileURL(fileID);
        return { fileId: fileID, tempUrl: tempUrl };
    } catch (error) {
        console.error('Error during operation:', error);
    }
}


